﻿using AutoMapper;
using FluentValidation;
using Mes.Application.DTO.Inputs.Operations;
using Mes.Application.Services.Operations;
using Mes.Application.Common;
using Microsoft.AspNetCore.Mvc;

namespace Mes.Application.Web.Controllers.Operations
{
    [ApiController]
    [Route("/api/v1")]
    public class StdOperController : ControllerBase
    {
        private readonly ILogger<StdOperController> _logger;
        private readonly IStdOperService _stdOperService;
        private readonly IMapper _mapper;
        private readonly IValidator<StdOperInput> _stdOperInputValidator;
        private readonly IValidator<StdOperUpdateInput> _stdOperUpdateInputValidator;
        private readonly IValidator<StdOperPageInput> _stdOperPageInputValidator;

        public StdOperController(ILogger<StdOperController> logger,
            IStdOperService stdOperService,
            IMapper mapper,
            IValidator<StdOperInput> stdOperInputValidator,
            IValidator<StdOperUpdateInput> stdOperUpdateInputValidator,
            IValidator<StdOperPageInput> stdOperPageInputValidator
            )
        {
            _logger = logger;
            _stdOperService = stdOperService;
            _mapper = mapper;
            _stdOperInputValidator = stdOperInputValidator;
            _stdOperUpdateInputValidator = stdOperUpdateInputValidator;
            _stdOperPageInputValidator = stdOperPageInputValidator;
        }

        /// <summary>
        /// 添加标准工序
        /// </summary>
        /// <param name="stdOperInput"></param>
        /// <returns></returns>
        [HttpPost("stdoper")]
        public async Task<ActionResult> AddStdOper(StdOperInput stdOperInput)
        {
            try
            {
                var input = await _stdOperInputValidator.ValidateAsync(stdOperInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _stdOperService.AddStandardOperationAsync(stdOperInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 编辑标准工序
        /// </summary>
        /// <param name="stdOperUpdateInput"></param>
        /// <returns></returns>
        [HttpPut("stdoper")]
        public async Task<ActionResult> UpdateStdOper(StdOperUpdateInput stdOperUpdateInput)
        {
            try
            {
                var input = await _stdOperUpdateInputValidator.ValidateAsync(stdOperUpdateInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _stdOperService.UpdateStandardOperationAsync(stdOperUpdateInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除标准工序
        /// </summary>
        /// <param name="oper_ids"></param>
        /// <returns></returns>
        [HttpDelete("stdoper")]
        public async Task<ActionResult> DeleteStdOper(string[] oper_ids)
        {
            try
            {
                await _stdOperService.DeleteStandardOperationAsync(oper_ids);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 分页查询标准工序列表
        /// </summary>
        /// <param name="stdOperPageInput"></param>
        /// <returns>StdOperResult</returns>
        [HttpGet("stdoper")]
        public async Task<ActionResult> GetStdOper([FromQuery] StdOperPageInput stdOperPageInput)
        {
            try
            {
                var input = await _stdOperPageInputValidator.ValidateAsync(stdOperPageInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                var result = await _stdOperService.GetStandardOperationsAsync(stdOperPageInput);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询指定标准工序
        /// </summary>
        /// <param name="oper_id"></param>
        /// <returns>PagedItemsData<StdOperResult></returns>
        [HttpGet("stdoper/entity")]
        public async Task<ActionResult> GetStdOperEntityByOperId(string oper_id)
        {
            try
            {
                var result = await _stdOperService.GetStandardOperationAsync(oper_id);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }
    }
}
